package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.mapper.TAccountMapper;
import com.neuedu.mapper.THealthMapper;
import com.neuedu.mapper.TRecordMapper;
import com.neuedu.pojo.*;
import com.neuedu.mapper.TElderMapper;
import com.neuedu.pojo.dto.elder.AddElderDTO;
import com.neuedu.pojo.dto.elder.CheckinElderDTO;
import com.neuedu.pojo.dto.elder.ElderNurseDTO;
import com.neuedu.service.ITElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.service.ITNurseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhangsan
 * @since 2024-06-16
 */
@Service
public class TElderServiceImpl extends ServiceImpl<TElderMapper, TElder> implements ITElderService {
    @Autowired
    TElderMapper elderMapper;
    @Autowired
    private TAccountMapper tAccountMapper;
    @Autowired
    private TRecordMapper tRecordMapper;
    @Autowired
    private ITNurseService nurseService;
    @Autowired
    private THealthMapper healthMapper;

    @Override
    public Result<List<TElder>> getPage(Integer currentPage, Integer pageSize, String eldName, Integer eldHealth, Integer eldBed) {
        Page<TElder> page = new Page<>(currentPage, pageSize);
        // 设置条件查询的构造器
        LambdaQueryWrapper<TElder> wrapper = new LambdaQueryWrapper<>();
        // 若条件为null，这个相当于全部查询
        wrapper.like(StringUtils.isNotBlank(eldName), TElder::getEldName, eldName)
                .eq(eldHealth != null, TElder::getEldHealth, eldHealth)
                .eq(eldBed != null, TElder::getEldBed, eldBed);
        elderMapper.selectPage(page, wrapper);
        return Result.success(page.getRecords(), "查询成功", page.getTotal());
    }

    @Override
    public Result findByElderId(int eldId) {
        return Result.success(elderMapper.selectById(eldId));

    }

    @Override
    public Result<List<TElder>> findByNurseId(Long nurseId, Long eldId, Boolean eldHealth) {
        LambdaQueryWrapper<TElder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TElder::getNurseId, nurseId)
                .eq( Objects.nonNull(eldId) , TElder::getEldId,eldId)
                .eq(Objects.nonNull(eldHealth) , TElder::getEldHealth, eldHealth);
        List<TElder> elders = elderMapper.selectList(lqw);
        return Result.success(elders, "查询成功");
    }

    @Override
    @Transactional
    public Result addElderAcc(AddElderDTO addElderDTO) {
        TAccount tAccount = new TAccount();
        BeanUtils.copyProperties(addElderDTO, tAccount);
        tAccount.setAccType(2);
        tAccount.setCreateTime(LocalDateTime.now());
        tAccount.setUpdateTime(LocalDateTime.now());
        tAccountMapper.insert(tAccount);
        TAccount tAccount1 = tAccountMapper.selectOne(new LambdaQueryWrapper<TAccount>()
                .eq(TAccount::getAccUsername, tAccount.getAccUsername()));
        TElder elder = new TElder();
        elder.setAccId(tAccount1.getAccId());
        elder.setEldState(1);
        elder.setCreateTime(LocalDateTime.now());
        elder.setUpdateTime(LocalDateTime.now());
        elderMapper.insert(elder);

        TElder elder1=elderMapper.selectOne(new LambdaQueryWrapper<TElder>()
                .eq(TElder::getAccId, elder.getAccId()));
        THealth health=new THealth();
        health.setEldId(elder1.getEldId());
        health.setCreateTime(LocalDateTime.now());
        health.setUpdateTime(LocalDateTime.now());
        healthMapper.insert(health);
        return Result.success(elder.getEldId(), "添加成功");
    }

    @Override
    @Transactional
    public Result checkinElder(CheckinElderDTO dto) {
        TElder tElder = new TElder();
        BeanUtils.copyProperties(dto, tElder);
        tElder.setUpdateTime(LocalDateTime.now());
        TElder tElder1 = elderMapper.selectOne(new LambdaQueryWrapper<TElder>()
                .eq(TElder::getAccId, tElder.getAccId()));
        tElder.setEldId(tElder1.getEldId());
        elderMapper.updateById(tElder);
        TRecord tRecord = new TRecord();
        tRecord.setRecordContents(dto.getRecordContents());
        tRecord.setElderId(tElder.getEldId());
        tRecord.setRecordState(0);
        tRecord.setTypeofrecord(dto.getTypeofrecord());
        tRecord.setUpdateTime(LocalDateTime.now());
        tRecord.setCreateTime(LocalDateTime.now());
        tRecordMapper.insert(tRecord);
        return Result.success("提交成功");
    }

    @Override
    public Result findByBed() {
        LambdaQueryWrapper<TElder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TElder::getEldBed,"").or().isNull(TElder::getEldBed);
        List<TElder> elders = elderMapper.selectList(wrapper);
        return Result.success(elders, "查询成功");
    }

    @Override
    public Result modBedByEldId(Integer eldId, String bedNumber) {
        LambdaUpdateWrapper<TElder> luw = new LambdaUpdateWrapper<>();
        luw.eq(TElder::getEldId, eldId)
                .set(TElder::getEldBed, bedNumber);

        return Result.success(elderMapper.update(null, luw));
    }

    @Override
    public Result getCheckinInfo(Long accId, String typeofrecord) {
        TElder tElder = elderMapper.selectOne(new LambdaQueryWrapper<TElder>()
                .eq(TElder::getAccId, accId));
        List<TRecord> tRecords = tRecordMapper.selectList(new LambdaQueryWrapper<TRecord>()
                .eq(TRecord::getElderId, tElder.getEldId())
                .eq(TRecord::getTypeofrecord, typeofrecord)
                .orderByDesc(TRecord::getUpdateTime));
        CheckinElderDTO checkinElderDTO = new CheckinElderDTO();
        BeanUtils.copyProperties(tElder, checkinElderDTO);
        if (!tRecords.isEmpty()) {

            BeanUtils.copyProperties(tRecords.get(0), checkinElderDTO);
        }
        return Result.success(checkinElderDTO, "查询成功");
    }

    @Override
    public Result findElderByBedNumber(String bedNumber) {
        LambdaQueryWrapper<TElder> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(TElder::getEldBed,bedNumber);
        return Result.success(elderMapper.selectOne(wrapper));
    }

    @Override
    public Result findAllWithNurse() {
        List<ElderNurseDTO> elderNurseDTOList = elderMapper.selectList(null).stream().map(tElder -> {
            ElderNurseDTO elderNurseDTO = new ElderNurseDTO();
            BeanUtils.copyProperties(tElder, elderNurseDTO);
            TNurse one = nurseService.getOne(new LambdaQueryWrapper<TNurse>()
                    .eq(TNurse::getNurseId, tElder.getNurseId()));
            if (one != null) {
                elderNurseDTO.setNurseName(one.getNurseName());
                elderNurseDTO.setNurseNumber(one.getNurseNumber());
                elderNurseDTO.setNurseId(one.getNurseId());
            }
            return elderNurseDTO;
        }).collect(Collectors.toList());
        return Result.success(elderNurseDTOList,"查询成功");
    }

    @Override
    public Result modNurseIdById(Integer nurseId, Integer eldId) {
        LambdaUpdateWrapper<TElder> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(TElder::getEldId,eldId)
                .set(TElder::getNurseId,nurseId);
        return Result.success(elderMapper.update(null,wrapper));
    }



    @Override
    public Result modElderById(TElder elder)
    {
      return Result.success(elderMapper.updateById(elder),"修改成功");
    }

    @Override
    public Result findByAccId(Long accId) {
        LambdaQueryWrapper<TElder> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TElder::getAccId, accId);
        List<TElder> elders = elderMapper.selectList(lqw);
        return Result.success(elders, "查询成功");
    }

    @Override
    public Result<List<TElder>> findAll() {
        return Result.success(elderMapper.selectList(null),"查询成功");
    }

}
